<?php

require_once dirname(__FILE__).'/../lib/jwGalleryAdminGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/jwGalleryAdminGeneratorHelper.class.php';

/**
 * jwGalleryAdmin actions.
 *
 * @package    estate
 * @subpackage jwGalleryAdmin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class jwGalleryAdminActions extends autoJwGalleryAdminActions
{
        
    public function executeUploadImage( sfWebRequest $request )
    {    
        $tmp_dir = sfContext::getInstance()->getUser()->getAttribute( 'tmp_dir', null,  'uploads' );
        if ( !$tmp_dir )
        {
            $tmp_dir = md5( time().rand()*10000 );
            sfContext::getInstance()->getUser()->setAttribute( 'tmp_dir', $tmp_dir, 'uploads' );
        }        
        
        $gal_dir = DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$tmp_dir;
        foreach( $_FILES as $file )
        {
            if( !is_dir( sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR."galleries" ) )
                mkdir( sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR."galleries" );
                
            if( !is_dir( sfConfig::get('sf_root_dir').$gal_dir ) )
                mkdir( sfConfig::get('sf_root_dir').$gal_dir );                

            $file['name'] = gdImage::convertFileName( $file['name'] );
                
            move_uploaded_file( $file['tmp_name'], sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR.$file['name'] );
            
            if( !is_dir( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs" ) )
                mkdir( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs" );
                
            $limits = sfConfig::get( 'app_jw_gallery' );
            $maxWidth = $maxHeight = $limits['edit'];
            
            if( !is_dir( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$maxWidth ) )
                mkdir( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$maxWidth );                            
            
            $thumb = new sfThumbnail( $maxWidth, $maxHeight );
            $thumb->loadFile( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR.$file['name'] );                
            $thumb->save( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$maxWidth.DIRECTORY_SEPARATOR.$file['name'] );

            $img = new sfThumbnail( $limits['max_size'], $limits['max_size'] );
            $img->loadFile( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR.$file['name'] );                
            $img->save( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR.$file['name'] );
            
            //rename( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."s_".$file['name'], sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR.$file['name'] );
            
            $pic = new jwGalleryPicture();
            $pic->setMime( gdImage::getMimeFromExtension( $file['name'] ) );
            $pic->setFileSize( $file['size'] );
            $pic->setFileName( $file['name'] );
            
            $tmp_key = md5( time().rand( 1000, 100000 ) );
            
            $picForm = new jwGalleryPictureForm( $pic );
            $ns = $this->getRequestParameter( 'namespace' );
            $nsm = preg_replace( "/\[[\w]+\]$/", "[pictures][".$tmp_key."][%s]", $ns );
            $picForm->getWidgetSchema()->setNameFormat( $nsm );

                
            return $this->renderText( $picForm );
        }
        
    }
    
    public function executeGetTempThumb( sfWebRequest $request )
    {
        header('Content-type: image/png');
        $tmp_dir = $this->getContext()->getUser()->getAttribute( 'tmp_dir', null, 'uploads' );
        $gal_dir = DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$tmp_dir;
        
        $limits = sfConfig::get( 'app_jw_gallery' );
        $maxWidth = $maxHeight = $limits['edit'];
        
        echo file_get_contents( sfConfig::get('sf_root_dir').$gal_dir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$maxWidth.DIRECTORY_SEPARATOR.$this->getRequestParameter( 'file' ) );
        return sfView::NONE;
    }
}
